package com.chinatelecom.ifree.business.dao.jifen.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.chinatelecom.ifree.business.dao.base.DbcpPool;
import com.chinatelecom.ifree.business.dao.invokelog.InvokeLogDao;
import com.chinatelecom.ifree.business.dao.jifen.IBonusActionDao;
import com.chinatelecom.ifree.business.dao.order.impl.BaseDaoSupport;
import com.chinatelecom.ifree.business.domain.jifen.BonusAction;
@Repository
public class BonusActionDaoImpl extends BaseDaoSupport<BonusAction> implements IBonusActionDao {
	
	private static transient Logger logger = LoggerFactory.getLogger(BonusActionDaoImpl.class) ;


	@Override
	public void save(BonusAction bonusAction) throws Exception {
		sql = "INSERT INTO bonus_action(name,description,status,maxTimesPerDay,bonus_calc_type , bonus_default_type,bonus_change_type,validation_code,uptime) VALUES(?,?,?,?,?,?,?,?,now())";
		
		super.saveEntity(bonusAction);
	}

	
	@Override
	public List<BonusAction> queryByMap(Map<String, Object> map) throws Exception {
		sql = "SELECT * FROM bonus_action ";
		Connection conn = null;
		PreparedStatement statement = null;
		ResultSet rs = null;
		List<BonusAction> list = new ArrayList<BonusAction>() ;
		try {
			if (map!=null && map.size()>0) {
				sql += " where ";
				Iterator<String> keyIterator = map.keySet().iterator() ;
				while (keyIterator.hasNext()) {
					String key = (String) keyIterator.next();
					sql += key+"=? " ;
				}
			}
			conn = DbcpPool.getInstance().getConn();
			statement = conn.prepareStatement(sql);
			if (map!=null && map.size()>0) {
				sql += " where ";
				Iterator<String> keyIterator = map.keySet().iterator() ;
				int i=0;
				while (keyIterator.hasNext()) {
					String key = (String) keyIterator.next();
					Object o = map.get(key) ;
					statement.setObject(++i, o);
				}
			}
			
			rs = statement.executeQuery();
			while (rs.next()) {
				BonusAction bonusAction = new BonusAction() ;
				bonusAction.setaId(rs.getInt("a_id"));
				bonusAction.setName(rs.getString("name"));
				bonusAction.setMaxTimesPerDay(rs.getInt("maxTimesPerDay"));
				bonusAction.setDescription(rs.getString("description"));
				bonusAction.setStatus(rs.getBoolean("status"));
				bonusAction.setBonusCalcType(rs.getInt("bonus_calc_type"));
				bonusAction.setBonusDefaultValue(rs.getInt("bonus_default_value"));
				bonusAction.setBonusChangeType(rs.getInt("bonus_change_type"));
				bonusAction.setValidationCode(rs.getString("validation_code"));
				bonusAction.setUpTime(rs.getDate("uptime"));
				
				list.add(bonusAction) ;
			}
			return list ;
		}finally{
			DbcpPool.getInstance().closeConnection(rs, statement, conn);
		}
		
	}

	@Override
	public BonusAction executeSingleResultSet(ResultSet rs) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void executeSql(PreparedStatement statement, BonusAction t)
			throws Exception {
		statement.setString(1, t.getName());
		statement.setString(2, t.getDescription());
		statement.setBoolean(3, t.isStatus());
		statement.setInt(4, t.getMaxTimesPerDay());
		statement.setInt(5, t.getBonusCalcType());
		statement.setInt(6, t.getBonusDefaultValue());
		statement.setInt(7, t.getBonusChangeType());
		statement.setString(8, t.getValidationCode());
	}


	@Override
	public void executeBatchSql(PreparedStatement state, BonusAction[] t)
			throws Exception {
		// TODO Auto-generated method stub
		
	}


	
}
